package cn.mengfly.fxtest.app;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;

public class ConstraintsGridPaneApp extends Application {

    @Override
    public void start(Stage primaryStage) {

        VBox vbox = new VBox();

        GridPane gp = new GridPane();
        gp.setPadding(new Insets(10));
        gp.setHgap(4);
        gp.setVgap(10);

        VBox.setVgrow(gp, Priority.ALWAYS);

        Label lblTitle = new Label("Support Ticket");

        Label lblEmail = new Label("Email");
        TextField tfEmail = new TextField();

        Label lblContract = new Label("Contract");
        TextField tfContract = new TextField();

        Label lblPriority = new Label("Priority");
        RadioButton rbMedium = new RadioButton("Medium");
        RadioButton rbHigh = new RadioButton("High");
        RadioButton rbLow = new RadioButton("Low");
        VBox priorityVbox = new VBox();
        priorityVbox.setSpacing(2);
        GridPane.setVgrow(priorityVbox, Priority.SOMETIMES);
        priorityVbox.getChildren().addAll(lblPriority, rbMedium, rbHigh, rbLow);

        Label lblSeverity = new Label("Severity");
        ObservableList<String> severities =
                FXCollections.observableArrayList("Blocker", "Workaround", "N/A");
        ComboBox<String> cbSeverity = new ComboBox<>(severities);

        Label lblCategory = new Label("Category");
        ObservableList<String> categories =
                FXCollections.observableArrayList("Bug", "Feature");
        ComboBox<String> cbCategory = new ComboBox<>(categories);

        Label lblProblem = new Label("Problem");
        TextField tfProblem = new TextField();

        Label lblDescription = new Label("Description");
        TextArea taDescription = new TextArea();

        gp.add(lblTitle, 0, 0);

        gp.add(lblEmail, 0, 1);
        gp.add(tfEmail, 0, 2);

        gp.add(lblContract, 0, 3);
        gp.add(tfContract, 0, 4);

        gp.add(priorityVbox, 0, 5);

        gp.add(lblSeverity, 1, 5);
        gp.add(cbSeverity, 1, 6);
        gp.add(lblCategory, 1, 7);
        gp.add(cbCategory, 1, 8);

        gp.add(lblProblem, 0, 9);
        gp.add(tfProblem, 0, 10);

        gp.add(lblDescription, 0, 11);
        gp.add(taDescription, 0, 12);

        GridPane.setColumnSpan(tfEmail, 2);
        GridPane.setColumnSpan(tfContract, 2);
        GridPane.setColumnSpan(tfProblem, 2);
        GridPane.setColumnSpan(taDescription, 2);

        GridPane.setRowSpan(priorityVbox, 4);

        // Constraints
        GridPane.setConstraints(taDescription,
                0, 12,
                2, 1,
                HPos.LEFT, VPos.TOP,
                Priority.SOMETIMES, Priority.ALWAYS);
        ColumnConstraints col1 = new ColumnConstraints();
        col1.setPercentWidth(50);
        ColumnConstraints col2 = new ColumnConstraints();
        col2.setPercentWidth(50);
        gp.getColumnConstraints().addAll(col1, col2);

        Separator sep = new Separator();

        ButtonBar buttonBar = new ButtonBar();
        buttonBar.setPadding(new Insets(10));

        Button saveButton = new Button("Save");
        Button cancelButton = new Button("Cancel");

        ButtonBar.setButtonData(saveButton, ButtonBar.ButtonData.OK_DONE);
        ButtonBar.setButtonData(cancelButton, ButtonBar.ButtonData.CANCEL_CLOSE);

        buttonBar.getButtons().addAll(saveButton, cancelButton);

        vbox.getChildren().addAll(gp, sep, buttonBar);

        Scene scene = new Scene(vbox);

        primaryStage.setTitle("Grid Pane App");
        primaryStage.setScene(scene);
        primaryStage.setWidth(414);
        primaryStage.setHeight(736);
        primaryStage.show();
    }
}
